package com.zhuiYun.project.api.menu.service.impl;

import com.zhuiYun.project.api.menu.entity.MenuEntity;
import com.zhuiYun.project.api.menu.mapper.MenuMapper;
import com.zhuiYun.project.api.menu.model.MenuModel;
import com.zhuiYun.project.api.menu.service.MenuService;
import com.zhuiYun.project.api.sysUser.entiy.SysUser;
import com.zhuiYun.project.common.errorCode.CommonException;
import com.zhuiYun.project.common.errorCode.EmErrorCode;
import com.zhuiYun.project.common.response.CommonResult;
import com.zhuiYun.project.util.BeanUtils;
import com.zhuiYun.project.util.UserUtils;
import com.zhuiYun.project.util.validator.ValidatorUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/** 菜单service实现
 * @Author dongdong
 * @Date 2023/4/3 20:03
 **/
@Service
@Slf4j
public class MenuServiceImpl implements MenuService {
    @Autowired
    MenuMapper menuMapper;
    /**
     * @description:菜单管理的分页查询方法
     * @author: dongdong
     * @date: 2023/4/4 13:33
     * @param: [menuModel, pageRequest]
     * @return: com.zhuiYun.project.common.response.CommonResult
     **/
    @Override
    public CommonResult getMenuAndFunction() {
        // 设置分页
//        Example example = new Example(SysUser.class);
        // 设置模糊查询
//        if(ObjectUtils.isNotEmpty(menuModel.getMenuName())){
//            example.createCriteria().andLike("userName","%"+menuModel.getMenuName()+"%");
//        }
        List<MenuEntity> menuAndFunctionList = menuMapper.selectAll();
        log.info(menuAndFunctionList.toString());
        for (MenuEntity menu : menuAndFunctionList) {
            if (menu.getParentId()==0){//判断是否最顶层类型
                updata(menu,menuAndFunctionList);//调用方法查找子类型
            }
        }
        for (int i = menuAndFunctionList.size()-1;i>=0;i--){
            if (menuAndFunctionList.get(i).getParentId()!=0){
                menuAndFunctionList.remove(i);
            }
        }
        return CommonResult.ok(menuAndFunctionList);
    }
    /**
     * @description:菜单新增
     * @author: dongdong
     * @date: 2023/4/4 13:33
     * @param: [menuModel]
     * @return: com.zhuiYun.project.common.response.CommonResult
    **/
    @Override
    public CommonResult saveMenu(MenuModel menuModel) throws CommonException {
        // 验证参数的规范性
        if(ValidatorUtil.ValidationObject(menuModel)){
            MenuEntity menuEntity = new MenuEntity();
            // sysUserModel 对象拷贝成 sysUser
            BeanUtils.copy(menuModel, menuEntity);
            menuEntity.setUpdateTime(new Date());
            menuEntity.setCreateTime(new Date());
            // 设置密码
            int i = menuMapper.insertSelective(menuEntity);
            if(i>0){
                return CommonResult.ok(EmErrorCode.SAVE_OK);
            }else{
                return CommonResult.ok(EmErrorCode.SAVE_ERROR);
            }
        }else{
            return CommonResult.ok(EmErrorCode.SAVE_ERROR);
        }
    }

    @Override
    public CommonResult getMenuNotFunction() {
        List<MenuEntity> menuNotFunction = menuMapper.getMenuNotFunction();
        for (MenuEntity menu : menuNotFunction) {
            if (menu.getParentId()==0){//判断是否最顶层类型
                updata(menu,menuNotFunction);//调用方法查找子类型
            }
        }
        for (int i = menuNotFunction.size()-1;i>=0;i--){
            if (menuNotFunction.get(i).getParentId()!=0){
                menuNotFunction.remove(i);
            }
        }
        return CommonResult.ok(menuNotFunction);
    }

    @Override
    public CommonResult delMenuById(Integer id) {
        return CommonResult.ok(menuMapper.deleteByPrimaryKey(id));
    }

    @Override
    public CommonResult updateMenu(MenuModel menuModel) throws CommonException {
        if(ValidatorUtil.ValidationObject(menuModel)){
            MenuEntity menu = new MenuEntity();
            // sysUserModel 对象拷贝成 sysUser
            BeanUtils.copy(menuModel, menu);
            int i = menuMapper.updateByPrimaryKeySelective(menu);
            if(i>0){
                return CommonResult.ok(EmErrorCode.UPDATE_OK);
            }else{
                return CommonResult.ok(EmErrorCode.UPDATE_ERROR);
            }
        }else{
            return CommonResult.ok(EmErrorCode.UPDATE_ERROR);
        }
    }

    @Override
    public CommonResult getMenuByUser() {
        SysUser user = UserUtils.getUser();
        List<MenuEntity> menuByUser = menuMapper.getMenuByUser(user.getId());
        for (MenuEntity menu : menuByUser) {
            if (menu.getParentId()==0){//判断是否最顶层类型
                updata(menu,menuByUser);//调用方法查找子类型
            }
        }
        for (int i = menuByUser.size()-1;i>=0;i--){
            if (menuByUser.get(i).getParentId()!=0){
                menuByUser.remove(i);
            }
        }
        return CommonResult.ok(menuByUser);
    }


    /**
     * @description:菜单递归方法
     * @author: dongdong
     * @date: 2023/4/4 13:34
     * @param: [carType, carTypes]
     * @return: void
    **/
    private static void updata(MenuEntity carType, List<MenuEntity> carTypes) {
        if (carType.getChildren()==null){
            carType.setChildren(new ArrayList<>());
        }
        for (MenuEntity type : carTypes) {//循环车类型集合查找当前类型子类型
            if (type.getParentId().equals(carType.getId())){
                List<MenuEntity> chiledren = carType.getChildren();
                chiledren.add(type);
                carType.setChildren(chiledren);
            }
        }
        if (carType.getChildren().size()!=0){//判断是否拥有子类型
            List<MenuEntity> chiledren = carType.getChildren();
            for (MenuEntity type : chiledren) {//循环子类型调用方法查找子类型的子类型
                updata(type,carTypes);
            }
            carType.setChildren(chiledren);
        }
    }
}
